package com.haha.helloworld;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DialogActivity extends AppCompatActivity {
    private Button btn_dialog_1;
    private Button btn_dialog_2;
    private Button btn_dialog_3;
    private Button btn_dialog_4;
    private Button btn_dialog_5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        btn_dialog_1 = (Button) findViewById(R.id.btn_dialog_1);
        btn_dialog_2 = (Button) findViewById(R.id.btn_dialog_2);
        btn_dialog_3 = (Button) findViewById(R.id.btn_dialog_3);
        btn_dialog_4 = (Button) findViewById(R.id.btn_dialog_4);
        btn_dialog_5 = (Button) findViewById(R.id.btn_dialog_5);
        OnClick onClick = new OnClick();
        btn_dialog_1.setOnClickListener(onClick);
        btn_dialog_2.setOnClickListener(onClick);
        btn_dialog_3.setOnClickListener(onClick);
        btn_dialog_4.setOnClickListener(onClick);
        btn_dialog_5.setOnClickListener(onClick);
    }

    class OnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_dialog_1:
                    AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                    builder.setTitle("请回答");
                    builder.setIcon(R.drawable.bg_fgo);
                    builder.setMessage("你觉得课程如何呀？");
                    //判断框
                    //设置一个点击事件
                    //积极按钮
                    builder.setPositiveButton("nice!", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getApplicationContext(), "你很诚实！", Toast.LENGTH_SHORT).show();
                        }
                        //中性按钮
                    }).setNeutralButton("还行", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getApplicationContext(), "你再瞅瞅！", Toast.LENGTH_SHORT).show();
                        }
                        //消极按钮
                    }).setNegativeButton("不好", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getApplicationContext(), "睁眼说瞎话！", Toast.LENGTH_SHORT).show();

                        }
                    }).show();
                    break;
                case R.id.btn_dialog_2:
                    //单选框版本
                    String[] array = new String[]{"男", "女"};
                    AlertDialog.Builder builder2 = new AlertDialog.Builder(DialogActivity.this);
                    builder2.setTitle("选择性别");
                    builder2.setItems(array, new DialogInterface.OnClickListener() {
                        @Override
                        //which表示的是选中的哪一个
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getApplicationContext(), "选中的是=>" + array[which], Toast.LENGTH_SHORT).show();
                        }
                    }).show();
                    break;
                case R.id.btn_dialog_3:
                    //单选按钮版本
                    String[] array2 = new String[]{"男", "女"};
                    AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this);
                    builder3.setTitle("请选择");
                    builder3.setSingleChoiceItems(array2, 0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getApplicationContext(), "选中的是=>" + array2[which], Toast.LENGTH_SHORT).show();
                            //点击选项，框框就消失
                            dialog.dismiss();
                        }
                        //setCancelable(false),表示的是点击空白区域，框框不会消失
                    }).show().setCancelable(false);
                    break;
                case R.id.btn_dialog_4:
                    String[] array3 = new String[]{"抽烟", "喝酒", "烫头"};
                    //默认选中谁，可以看出是选中的第三个
                    boolean[] isSelected = new boolean[]{false, false, true};
                    AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this);
                    builder4.setTitle("选择兴趣");
                    builder4.setMultiChoiceItems(array3, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                            Toast.makeText(getApplicationContext(), "选中的是=>" + array3[which], Toast.LENGTH_SHORT).show();

                        }
                    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getApplicationContext(), "确定！", Toast.LENGTH_SHORT).show();

                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getApplicationContext(), "取消" , Toast.LENGTH_SHORT).show();

                        }
                    }).show();
                    break;
                    case R.id.btn_dialog_5:
                        AlertDialog.Builder builder5 = new AlertDialog.Builder(DialogActivity.this);
                        //定义布局填充器
                        View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog, null);
                        //定义相关组件
                        EditText et_username =(EditText) view.findViewById(R.id.et_username);
                        EditText et_password =(EditText) view.findViewById(R.id.et_password);
                        Button btn_login =(Button) view.findViewById(R.id.btn_login);
                        btn_login.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(getApplicationContext(), "登录事件触发" , Toast.LENGTH_SHORT).show();
                            }
                        });
                        builder5.setTitle("请先登录");
                        //将设置的view导入
                        builder5.setView(view);
                        builder5.show();
                        break;
            }

        }
    }
}